<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends BaseController {
	//权限认证检测
	public function index(){
		session_start();
		$uname=$_SESSION['username'];
		$this -> assign("user", "$uname");
		$this -> display();
	}
	
	//个人信息的函数
	public function info() {
		if(IS_POST){
			if($_POST['info']=='tuichu'){
				session_unset();
				header("Location:http:/Home/Index/login");
			}
		   session_start();
		   $uname=$_SESSION['username'];
		   $m=M("Member");
		   $res=$m->where("username='{$uname}'")->select();
		   $name=$res[0]["name"];	
		   $phone=$res[0]["phone"];	
		   $riqi=$res[0]["birthday"];
		   $data['name'] = $_POST['infoname'];
			$data['id'] = $_POST['infoid'];
            $data['phone'] = $_POST['infophone'];
            $data['birthday'] = $_POST['inforiqi'];	
			if($name==$_POST['infoname']&&$phone==$_POST['infophone']&&$riqi==$_POST['inforiqi']){
			$obj=(object)[];
			$obj->code=1;
			$obj->message="修改失败";
			$this->ajaxReturn($obj);
			}else{
			$m->save($data); // 根据条件保存修改的数据
			$obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
			}
		}else{
			session_start();
		$uname=$_SESSION['username'];
		$m=M("Member");
		$res=$m->where("username='{$uname}'")->select();
		  $id=$res[0]["id"];	
		  $name=$res[0]["name"];	
		  $phone=$res[0]["phone"];	
		  $riqi=$res[0]["birthday"];	
		$this -> assign("user", "$uname");
		$this -> assign("id", "$id");
		$this -> assign("name", "$name");
		$this -> assign("phone", "$phone");
		$this -> assign("birthday", "$riqi");
		$this -> display();
		}
	}
         // 修改密码的函数
	public function updatePwd() {
		if(IS_POST){
			if($_POST['info']=='tuichu'){
				session_unset();
				header("Location:http:/Home/Index/login");
			}
			$m=M("Member");
			$data['id'] = $_POST['updateid'];
            $data['password'] = md5($_POST['querenpassword']);
            $m->save($data); // 根据条件保存修改的数据
			$obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
		}else{
			session_start();
		    $uname=$_SESSION['username'];
			$m=M("Member");
		    $res=$m->where("username='{$uname}'")->select();
		    $id=$res[0]["id"];
			$this -> assign("user", "$uname");
		    $this -> assign("id", "$id");
		    $this -> display();
		}
	}
	
	public function auth() {
		if($_POST['info']=='tuichu'){
				session_unset();
				header("Location:http:/Home/Index/login");
			}
		    session_start();
		    $uname=$_SESSION['username'];
			$this -> assign("user", "$uname");
		    $this -> display();
	}
	
	public function role() {
		if($_POST['info']=='tuichu'){
				session_unset();
				header("Location:http:/Home/Index/login");
			}
		    session_start();
		    $uname=$_SESSION['username'];
			$this -> assign("user", "$uname");
		    $this -> display();
	}
	
	public function items() {
		if(IS_POST){
			if($_POST['info']=='tuichu'){
				session_unset();
				header("Location:http:/Home/Index/login");
			};
			if($_POST["hiddenids"]=="123"){
				$data['username'] =$_POST["username"];
			$data['password'] =md5($_POST["itemspassword"]);
			$data['name'] = $_POST["itemsname"];
			$data['phone'] = $_POST["itemsphone"];
			$data['birthday'] = $_POST["itemsriqi"];
			$data['sex'] = $_POST["itemssex"];
			$Form = D('Member');
            $Form->add($data);
            $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
			}
			if($_POST["hiddenid"]=="111"){
			 $unames=$_POST["id"];
			 $form=M("Member");
		     $res=$form->where("username='{$unames}'")->select();
		     $name=$res[0]["name"];	
		     $phone=$res[0]["phone"];	
		     $riqi=$res[0]["birthday"];
		     $sex=$res[0]["sex"];
		     $data['id']=$res[0]["id"];	
			$data['name'] = $_POST["itemsname"];
			$data['phone'] = $_POST["itemsphone"];
			$data['birthday'] = $_POST["itemsriqi"];
			$data['sex'] = $_POST["itemssex"];
			 if($name==$_POST["itemsname"]&&$phone==$_POST["itemsphone"]&&$riqi==$_POST["itemsriqi"]&&$sex==$_POST["itemssex"]){
			 	$obj=(object)[];
			$obj->code=1;
			$obj->message="修改失败";
			$this->ajaxReturn($obj);
			 }else{
            $form->save($data);
            $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
			 }
			}
			if($_POST["hiduid"]=="0123"){
			 $unames=$_POST["delid"];
			 $form = M('Member');
			 $res=$form->where("username='{$unames}'")->select();
		     $id=intval($res[0]["id"]);	
			 if($id==1){
			 	$obj=(object)[];
			    $obj->code=1;
			    $obj->message="修改失败";
			    $this->ajaxReturn($obj);
			 }else{
			 	$form->where('id='.$id)->delete();
			    $obj=(object)[];
			    $obj->code=0;
			    $obj->message="修改成功";
			    $this->ajaxReturn($obj);
			 }
			}
		}else{
			session_start();
		    $uname=$_SESSION['username'];
			$this -> assign("user", "$uname");
		    $this -> display();
		}
	}
	public function listitem(){
		$ret=(object)[];
		$ret->data=array();
		for($i=0;$i<100;$i++){
			$arr=array($i,"姓名".rand(0, 100),rand(0, 1)?"男":"女",rand(10, 98));
		    $arr[]='<div class="btn-group"><button type="button" class="btn btn-warning">编辑</button><button type="button" class="btn btn-default">删除</button><button type="button" class="btn btn-danger">权限</button></div>';
		    $ret->data[]=$arr;
		};
		echo json_encode($ret);
	}
    public function roleitem(){
		$ret=(object)[];
		$ret->data=array();
		for($i=0;$i<100;$i++){
			$arr=array($i,"姓名".rand(0, 100),rand(0, 1)?"男":"女",rand(10, 98));
		    $arr[]='<div class="btn-group"><button type="button" class="btn btn-warning">编辑</button><button type="button" class="btn btn-default">删除</button><button type="button" class="btn btn-danger">权限</button></div>';
		    $ret->data[]=$arr;
		};
		echo json_encode($ret);
	}
	public function authitem(){
		$ret=(object)[];
		$ret->data=array();
		for($i=0;$i<100;$i++){
			$arr=array($i,"姓名".rand(0, 100),rand(0, 1)?"男":"女",rand(10, 98));
		    $arr[]='<div class="btn-group"><button type="button" class="btn btn-warning">编辑</button><button type="button" class="btn btn-default">删除</button><button type="button" class="btn btn btn-danger">权限</button></div>';
		    $ret->data[]=$arr;
		};
		echo json_encode($ret);
	}
	
	
	/**
	 * 用户列表查询伪数据排序
	 * 
	 */
	
	public function serverlist(){
		//内容
		$list=array();
		for($i=1;$i<=1000;$i++){
			$arr=array($i,"姓名-".$i,rand(0, 1)?"男":"女",rand(10, 98));
		    $arr[]='<div class="btn-group"><button type="button" class="btn btn-warning">编辑</button><button type="button" class="btn btn-default">删除</button><button type="button" class="btn btn btn-danger">权限</button></div>';
		    $list[]=$arr;
		}
		if(IS_GET){
			$columnindex=$_GET['order'][0]['column'];
			$dir=$_GET['order'][0]['dir'];
			$start=$_GET['start'];
			$length=$_GET['length'];
			$search=$_GET['search']['value'];
			$ret=(object)[];
			$ret->data=array();
			$ret->draw=$_GET['draw'];
			$count;
			for($i=0;$i<count($list);$i++){
//				$search==""?true:false||$list[$i][0]==$serach
				if(strchr($list[$i][2],$search)>=0){
							$count++;
						if($i>=$start && count($ret->data)<$length){
					       $ret->data[]=$list[$i];
						}
				}
			}
			$ret->recordsFiltered=$count;
			$ret->recordsTotal=1000;
			$this->ajaxReturn($ret);
		}
	}
	/**
	 * 用户列表查询数据库
	 * 
	 */
	 public function serverdb(){
	 	$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M("Member");
		if($columnindex==1){
			$column="username";
		}
       else if($columnindex==2){
			$column="name";
		}
        else if($columnindex==3){
			$column="sex";
		}
		else if($columnindex==4){
			$column="birthday";
		}else if($columnindex==5){
			$column="phone";
		}else{
			$column="jibie";
		}
		$res=$m->where("username like '%{$search}%' or name like '%{$search}%' or sex like '%{$search}%' or birthday like '%{$search}%' or phone like '%{$search}%' or jibie like '%{$search}%'")->field("username,name,sex,birthday,phone,jibie")->order($column." ".$dir)->limit($start,$length)->select();;
		$ret=(object)[];
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$str='<div class="btn-group"><button type="button" class="btn btn-default btn-bj" data-toggle="modal" data-target="#modal-update" data-id="'.$res[$i]["username"].'">编辑</button><button type="button" class="btn btn-default btn-del" data-toggle="modal" data-target="#modal-del" data-id="'.$res[$i]["username"].'">删除</button></div>';
			$ret->data[]=array($i+1,$res[$i]["username"],$res[$i]["name"],$res[$i]["sex"],$res[$i]["birthday"],$res[$i]["phone"],$res[$i]["jibie"],$str);
		}
		$ret->recordsFiltered=100;
		$ret->recordsTotal=100;
		$this->ajaxReturn($ret);
	    
	 }
     
	 /**
	 * 角色列表查询数据库
	 * 
	 */
	 public function roledb(){
	 	$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		$m=M("auth_group");
		if($columnindex==1){
			$column="title";
		}
		else if($columnindex==2){
			$column="beizhu";
		}else{
			$column="id";
		}
		$res=$m->where("title like '%{$search}%'")->field("id,title,beizhu")->order($column." ".$dir)->limit($start,$length)->select();;
		
		$ret=(object)[];
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($res);$i++){
			$ret->data[]=array($i+1,$res[$i]["title"],"可用",$res[$i]["beizhu"],'<button type="button" class="btn btn-default btn-del" data-toggle="modal" data-target="#modal-del" data-id="'.$res[$i]["id"].'" ">删除</button>');
		}
		$ret->recordsFiltered=10;
		$ret->recordsTotal=10;
		$this->ajaxReturn($ret);
	    
	 }
	 /**
	 * 角色列表查询数据库
	 * 
	 */
	 public function rolefen(){
	 	$id=$_POST['dom'];
	 	$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		if($columnindex==1){
			$column="username";
		}
        else if($columnindex==2){
			$column="name";
		}
		else{
			$column="id";
		}
		$aga = M('member');
		$group=$aga->field("username,name")->join("think_auth_group_access on think_auth_group_access.uid=think_member.id") -> where("group_id='{$id}'")->order($column." ".$dir)->limit($start,$length) -> select();
		$ret=(object)[];
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($group);$i++){
			$ret->data[]=array($i+1,$group[$i]["username"],$group[$i]["name"]);
		}
		$ret->recordsFiltered=count($group);
		$ret->recordsTotal=10;
		$this->ajaxReturn($ret);
//      var_dump($group);
	    
	 }
	 /**
	 * 角色列表插入数据库
	 * 
	 */
	 public function addroles(){
	 	if(IS_POST){
	 		if($_POST["name"]=="用户"||$_POST["name"]=="管理员"||$_POST["name"]=="普通用户"){
	 			$data['title'] =$_POST["name"];
	 		}else{
	 			$obj=(object)[];
			$obj->code=1;
			$obj->message="插入失败";
			$this->ajaxReturn($obj);
	 		}
	 		if($_POST["name"]=="超级管理员"){
	 	    $obj=(object)[];
			$obj->code=1;
			$obj->message="插入失败";
			$this->ajaxReturn($obj);
	 		}
			if($_POST["rolesyong"]=="可用"){
			$data['status'] = 1;
			}else{
			$data['status'] = 0;
			}
			if($_POST["name"]=="管理员"){
				$data['rules']="1".","."2".","."3".","."4".","."5".","."8";
			}
			if(!$_POST["name"]=="管理员"&&$_POST["name"]=="超级管理员"){
				$data['rules']="1".","."3".","."4";
			}
			$data['beizhu'] = $_POST["roleshuo"];
			$Form = D('auth_group');
            $Form->add($data);
            $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
	 	}
	 }
	 //删除数据左边表
	 public function delformrole(){
	 	if(IS_POST){
			 $uid=$_POST["delid"];
			 $usming=$_POST["hiduid"];
			 $form = M('auth_group');
			 $res=$form->where("id='{$uid}'")->find();
			    $m = M('auth_group_access');
				$cd=$m->field("uid")->where("group_id='{$uid}'")->select();
			    if($cd){
			    	$obj=(object)[];
			        $obj->code=1;
			        $obj->message="不能删除";
			        $this->ajaxReturn($obj);
			    }else{
			    	$form->where('id='.$uid)->delete();
			    $obj=(object)[];
			    $obj->code=0;
			    $obj->message="修改成功";
			    $this->ajaxReturn($obj);
			    }
	 }
	 }
	 //删除右边表数据
	 public function delallin(){
	 	if(IS_POST){
			 $uid=$_POST["hiduid1"];
			 $usming=$_POST["delid1"];
			 $form = M('member');
			 $res=$form->where("username='{$uid}' and name='{$usming}'")->delete();
			    $obj=(object)[];
			    $obj->code=0;
			    $obj->message="修改成功";
			    $this->ajaxReturn($obj);
//			    $this->ajaxReturn($res);
	 }
	 }
/**
	 * 权限列表查询数据库
	 * 
	 */
	 public function authdb(){
	 	$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		if($columnindex==1){
			$column="title";
		}
		else if($columnindex==2){
			$column="beizhu";
		}else{
			$column="uid";
		}
		$aga = M('auth_group_access');
		$group=$aga->field("title,beizhu,uid")->join("think_auth_group on think_auth_group_access.group_id=think_auth_group.id") -> where("title like '%."+$search+".% or uid like '%."+$search+".%")->order($column." ".$dir)->limit($start,$length) -> select();
		$ret=(object)[];
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($group);$i++){
			$ret->data[]=array($i+1,$group[$i]["title"],$group[$i]["beizhu"],$group[$i]["uid"]);
		}
		$ret->recordsFiltered=10;
		$ret->recordsTotal=10;
		$this->ajaxReturn($ret);
	    
	 }
	 /**
	 * 权限列表查询数据库
	 * 
	 */
	 public function authfen(){
	 	$id=$_POST['dom'];
	 	$columnindex=$_GET['order'][0]['column'];
		$dir=$_GET['order'][0]['dir'];
		$start=$_GET['start'];
		$length=$_GET['length'];
		$search=$_GET['search']['value'];
		if($columnindex==1){
			$column="title";
		}
		else{
			$column="id";
		}
		$M = M('auth_group');
		$retd=$M->field('rules')->where("id=".$id)->find();
		$rid=$retd['rules'];
		$Model = M('auth_rule');
		$group=$Model->field('title,id')->where("id in(".$rid.")")->select();
		$ret=(object)[];
		$ret->data=array();
		$ret->draw=$_GET['draw'];
		$ret->search=$search;
		for($i=0;$i<count($group);$i++){
			$ret->data[]=array($i+1,$group[$i]["title"],$group[$i]["id"]);
		}
		$ret->recordsFiltered=count($group);
		$ret->recordsTotal=10;
		$this->ajaxReturn($ret);
	 }
	 
	 public function authdel(){
	 	$auid=$_POST['auid'];
	 	$uid=$_POST['uid'];
		if($auid==1||$auid==2){
			    $obj=(object)[];
			    $obj->code=1;
			    $obj->message="删除失败";
			    $this->ajaxReturn($obj);
		}else{
		$M = M('auth_group');
		$retd=$M->field('rules')->where("id=".$uid)->find();
		if($auid==3&&$retd["rules"]=="1,3,4"){
		$data['id'] = $uid;
        $data['rules'] = "1".","."4";
        $M->save($data); // 根据条件保存修改的数据
        $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
		}
		if($auid==3&&$retd["rules"]=="1,3"){
		$data['id'] = $uid;
        $data['rules'] = "1";
        $M->save($data); // 根据条件保存修改的数据
        $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
		}
		if($auid==4&&$retd["rules"]=="1,3,4"){
		$data['id'] = $uid;
        $data['rules'] = "1".","."3";
        $M->save($data); // 根据条件保存修改的数据
        $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
		}
		if($auid==4&&$retd["rules"]=="1,4"){
		$data['id'] = $uid;
        $data['rules'] = "1";
        $M->save($data); // 根据条件保存修改的数据
        $obj=(object)[];
			$obj->code=0;
			$obj->message="修改成功";
			$this->ajaxReturn($obj);
		}
		}
	 }
}
?>